//
//  Consult.m
//  HappyStore
//
//  Created by 点点充 on 16/1/20.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "Consult.h"
#import "ConsultCell.h"
#import "ConsultDetail.h"
@interface Consult ()<UIWebViewDelegate>
{
    NSMutableArray *_aryInfo;
    NSInteger paggge;
    
    NSMutableDictionary *muDicParameters;
    
    NSString *strProjectId;
    
}

@end

@implementation Consult
static NSString *cellConsult = @"ConsultHomeCell";
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"白色背景"] forBarMetrics:UIBarMetricsDefault];

}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.textColor = RGB(45, 45, 45);
    titleLabel.text = @"政商参考";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"返回" highImage:@"返回"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    //self.mainTableView.bounces = NO;
    _aryInfo = [NSMutableArray array];
    paggge = 1;
    
    
    
    UINib *nib = [UINib nibWithNibName:@"ConsultCell" bundle:nil];
    [_mainTableView registerNib:nib forCellReuseIdentifier:cellConsult];
    
    _mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (_aryInfo.count != 0) {
            if ([[USER_DEFAULTS objectForKey:@"isOpenSound"] boolValue]) {//声音开关
                NSString *path = [[NSBundle mainBundle]pathForResource:@"refresh"
                                                                ofType:@"wav"];
                SystemSoundID soundID;
                AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
                AudioServicesPlaySystemSound(soundID); //仅播放音频
            }
            paggge = 1;
            [muDicParameters setObject:@"1" forKey:@"page"];
            [self startSearch];
        }else {
            [_mainTableView.mj_header endRefreshing];
        }
    }];
    
    
    
    _mainTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        paggge += 1;
        [muDicParameters setObject:[NSString stringWithFormat:@"%ld",paggge] forKey:@"page"];
        [self startSearch];
    }];
    
    _mainTableView.mj_footer.hidden = YES;
    
    if (muDicParameters == nil) {
        muDicParameters = [NSMutableDictionary dictionary];
        [muDicParameters setObject:@"1" forKey:@"page"];
    }
    
    [self showRoundProgressWithTitle:@"请稍后..."];
    [self startSearch];
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}

- (void)leftBarButtonItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
    return _aryInfo.count;
   
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        return 104;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    ConsultCell *cell = [tableView dequeueReusableCellWithIdentifier:cellConsult];
    
    [cell.photo sd_setImageWithURL:[NSURL URLWithString:_aryInfo[indexPath.row][@"photos"][0]] placeholderImage:[UIImage imageNamed:@"img_normal3"]];
    cell.titleLabel.text = _aryInfo[indexPath.row][@"title"];
    
    NSDate *date = [CustomExcetion dateFromString:_aryInfo[indexPath.row][@"createTime"]];
    cell.webLabel.text = _aryInfo[indexPath.row][@"releaseName"];
    cell.timeLabel.text = [CustomExcetion prettyDateWithReference:date];
    


    
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];//取消选中状态
        //取消显示后退键文字
        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
        self.navigationItem.backBarButtonItem = barButtonItem;
        
        
        
        ConsultDetail *Convc = [[ConsultDetail alloc] initWithNibName:@"ConsultDetail" bundle:nil];
        Convc.projectId = _aryInfo[indexPath.row][@"id"];
        Convc.timeStr = _aryInfo[indexPath.row][@"createTime"];
        Convc.titleStr = _aryInfo[indexPath.row][@"title"];
        [self.navigationController pushViewController:Convc animated:YES];
    
    
    

    
    
}


//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return 0;
//}
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
//{
//    // segue.identifier：获取连线的ID
//    if ([segue.identifier isEqualToString:@"consult_detail"]) {
//        Consult_Detail *result = segue.destinationViewController;
//        result.projectId = strProjectId;
//    }
//}


- (void)startSearch
{    
    NSString *strURl = [NSString stringWithFormat:@"%@%@",URL_DEFINE,@"getHaving_app_having.im"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:strURl parameters:muDicParameters progress:^(NSProgress * _Nonnull uploadProgress) {
       
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        
        if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
            
            if (paggge == 1) {
                [_mainTableView.mj_header endRefreshing];
                
                
                _aryInfo = [NSMutableArray arrayWithArray:[dicInfo objectForKey:@"data"][@"record"]];
                
               
                if (_aryInfo.count == 10) {
                    _mainTableView.mj_footer.hidden = NO;
                }else {
                    _mainTableView.mj_footer.hidden = YES;
                }
                
            }else {
                [_mainTableView.mj_footer endRefreshing];
                NSArray *ary11 = [dicInfo objectForKey:@"data"][@"record"];
                for (int i = 0; i<ary11.count; i++) {
                    NSDictionary *dic1 = ary11[i];
                    [_aryInfo addObject:dic1];
                }
                if (ary11.count == 10) {
                    _mainTableView.mj_footer.hidden = NO;
                }else {
                    _mainTableView.mj_footer.hidden = YES;
                }
                
            }
            
            
            [_mainTableView reloadData];
            [self hideBubble];
        }else {
            [_mainTableView reloadData];
            [_mainTableView.mj_header endRefreshing];
            [_mainTableView.mj_footer endRefreshing];

            [self showSingleIconBubble:[dicInfo objectForKey:@"msg"]];

        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [_mainTableView.mj_header endRefreshing];
        [_mainTableView.mj_footer endRefreshing];
        [self showSingleIconBubble:error.localizedDescription];

    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//-(NSString *)str:(NSString *)string{
//    UIWebView *web = [[UIWebView alloc] init];
//    web.delegate = self;
//    
//    
//    
//}


@end
